package com.ncloud.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.crm.common.Constant;
import com.ncloud.crm.entity.order.OrderContractNode;
import com.ncloud.crm.mapper.OrderContractNodeMapper;
import com.ncloud.crm.service.OrderContractNodeService;
import com.ncloud.crm.utils.DateUtils;
import com.ncloud.crm.utils.TableIdDefineUtils;

/**
 * Created by shiqiang on 2017/4/21.
 */
@Service
public class OrderContractNodeServiceImpl implements OrderContractNodeService {
    @Autowired
    private OrderContractNodeMapper orderContractNodeMapper;
    @Override
    public int insert(OrderContractNode orderContractNode) {
        return orderContractNodeMapper.insertContractNode(orderContractNode);
    }
    @Override
    public void getContractNodeRelation(JSONObject node, JSONObject param,JSONArray array){
        if(node.containsKey("modifyTag")){
            OrderContractNode contractNode = new OrderContractNode();
            contractNode.setModifyTag(node.getString("modifyTag"));
            contractNode.setOrderId(param.getLong(TableIdDefineUtils.ORDER_ID));
            contractNode.setChildContractInstId(param.getLong(TableIdDefineUtils.CHILD_CONTRACT_ID));
            contractNode.setNodeId(param.getLong(TableIdDefineUtils.NODE_ID));
            contractNode.setBeginDate(DateUtils.parseDate(param.getString("createDate")));
            contractNode.setEndDate(DateUtils.parseDate(Constant.END_DATE));
            contractNode.setUpdateTime(DateUtils.parseDate(param.getString("createDate")));
            contractNode.setUpdateUser(param.getLong("userId"));
            array.add(JSON.parseObject(JSON.toJSONString(contractNode)));
        }else{
            throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"节点信息modifyTag不存在!");
        }
    }
    
    @Override
	public List<OrderContractNode> getOrderContractNodesByOrderId(Long orderId) {
		
		return orderContractNodeMapper.getOrderContractNodesByOrderId(orderId);
	}
}
